package com.ouken.asynTask;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.tool.AlertDialog_PG;
import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.com.LoginActivity;
import com.ouken.com.R;
import com.ouken.tool.ConnectServer;
import com.ouken.tool.MyApp;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class AsynResetpwd extends AsyncTask<String, TextView, String> {

	private Context context;
	private WifiManager wifi;
	private List params;
	private String httpUrl;
	private MyApp appState;
	//private ProgressDialog dialog;
	private String email;
	private EditText code;
	private ImageButton sendcode;
	private EditText et_forgetpwd_email;
	
	private AlertDialog_PG PGDialog;
	private AlertDialog PGnow;

	public AsynResetpwd(Context context, MyApp appState,ImageButton sendcode,  EditText code, EditText et_forgetpwd_email) {
		super();
		this.context = context;
		this.wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
		this.params = new ArrayList();
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/forgetpassword";
		this.appState = appState;
		//this.dialog = new ProgressDialog(context);
		this.email = et_forgetpwd_email.getText().toString();
		this.code = code;
		this.sendcode = sendcode;
		this.et_forgetpwd_email =  et_forgetpwd_email;
		
		PGDialog = new AlertDialog_PG(context,"Password resetting...");
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		System.out.println("AsynResetpwd onpreexcute");

		final AlertDialog PGnow1 = PGDialog.AlertDialog_PG_show();
		PGnow = PGnow1;
        /*dialog.setMessage("Password resetting...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();*/
        
		params.add(new BasicNameValuePair("email", email.toString()));
		params.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
		
	}
	@Override
	protected String  doInBackground(String...strings ) {
		System.out.println("AsynResetpwd doInBackground");
		return ConnectServer.connect(httpUrl,params);
		
	}

	@Override
    protected void onPostExecute(String result) {
		System.out.println("AsynResetpwd onPostExecute");
		
		final AlertDialog PGnow_c = PGnow;
		
		if(result == null){
			PGnow_c.dismiss();
			 //dialog.dismiss();
			 AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("ERROR!")
	                .setMessage(R.string.ERROR_NETWORKOFF)
	                .setPositiveButton("OK",null)
	                .create();
				
			alertDialog.show();
			
			return;
		}
		String[] resultParams =result .split(",");
		
		if(resultParams[0].toString().contains("OK")){
			
			PGnow_c.dismiss();
			//dialog.dismiss();
			
			AlertDialog_ok myDialog = new AlertDialog_ok(context,"Success!\nA code has been \nsent to your email");
        	final AlertDialog now = myDialog.AlertDialog_ok_show();
		    
        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
				@Override  
			    public void onClick(View v) {  
					SoundPlayer.playSound(R.raw.keypress_common);
					sendcode.setVisibility(View.VISIBLE);
        			code.setVisibility(View.VISIBLE);
        			et_forgetpwd_email.setClickable(false);
        			
        			et_forgetpwd_email.setFilters(new InputFilter[] { new InputFilter() {

						@Override
						public CharSequence filter(CharSequence source,
								int start, int end, Spanned dest,
								int dstart, int dend) {
							// TODO Auto-generated method stub
							return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
						}
        				} }); 
					} 
				}
			);  
			/* AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("Success!")
	                .setMessage("A code has been sent to your email")
	                .setPositiveButton("OK",new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	sendcode.setVisibility(View.VISIBLE);
	            			code.setVisibility(View.VISIBLE);
	            			et_forgetpwd_email.setClickable(false);
	            			
	            			et_forgetpwd_email.setFilters(new InputFilter[] { new InputFilter() {

								@Override
								public CharSequence filter(CharSequence source,
										int start, int end, Spanned dest,
										int dstart, int dend) {
									// TODO Auto-generated method stub
									return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
								}
	            				} });
	            			
	            			
	            			
	                    }
	                })
	                .create();
				
			alertDialog.show();*/
			
			
			
		}else{
			PGnow_c.dismiss();
			//dialog.dismiss();
			AlertDialog_ok myDialog = new AlertDialog_ok(context,resultParams[1]);
        	final AlertDialog now = myDialog.AlertDialog_ok_show();
		    
        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
				@Override  
			    public void onClick(View v) {  
					SoundPlayer.playSound(R.raw.keypress_common);
					now.dismiss();
        			
					} 
				}
			);  
			/*AlertDialog alertDialog =
				new AlertDialog.Builder(context)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle("ERROR!")
                .setMessage(resultParams[1])
                .setPositiveButton("OK",null)
                .create();
			
				alertDialog.show();*/
		}
		
    }
	

	

	
	
	
}
